<?php
// 开启会话
session_start();

// 初始化店铺数据
if (!isset($_SESSION['money'])) {
    $_SESSION['money'] = 1000; // 初始资金
    $_SESSION['products'] = [
        [
            'name' => '苹果',
            'price' => 5, // 销售价格
            'cost' => 3,  // 进货价格
            'stock' => 0
        ],
        [
            'name' => '香蕉',
            'price' => 4,
            'cost' => 2,
            'stock' => 0
        ]
    ];
}

// 处理进货操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['purchase'])) {
    $productIndex = (int)$_POST['product_index'];
    $quantity = (int)$_POST['quantity'];
    $product = $_SESSION['products'][$productIndex];
    $totalCost = $product['cost'] * $quantity;
    if ($_SESSION['money'] >= $totalCost) {
        $_SESSION['money'] -= $totalCost;
        $_SESSION['products'][$productIndex]['stock'] += $quantity;
        $message = "成功进货 {$quantity} 个 {$product['name']}，花费 {$totalCost} 元。";
    } else {
        $message = "资金不足，无法进货。";
    }
}

// 处理销售操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sell'])) {
    $productIndex = (int)$_POST['product_index'];
    $quantity = (int)$_POST['quantity'];
    $product = $_SESSION['products'][$productIndex];
    if ($product['stock'] >= $quantity) {
        $totalIncome = $product['price'] * $quantity;
        $_SESSION['money'] += $totalIncome;
        $_SESSION['products'][$productIndex]['stock'] -= $quantity;
        $message = "成功销售 {$quantity} 个 {$product['name']}，收入 {$totalIncome} 元。";
    } else {
        $message = "库存不足，无法销售。";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>店铺经营网页游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        th,
        td {
            border: 1px solid #ccc;
            padding: 10px;
            text-align: center;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input[type="number"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>店铺经营网页游戏</h1>
    <p>当前资金: <?php echo $_SESSION['money']; ?> 元</p>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <h2>商品库存</h2>
    <table>
        <thead>
            <tr>
                <th>商品名称</th>
                <th>销售价格</th>
                <th>进货价格</th>
                <th>库存数量</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($_SESSION['products'] as $index => $product): ?>
                <tr>
                    <td><?php echo $product['name']; ?></td>
                    <td><?php echo $product['price']; ?> 元</td>
                    <td><?php echo $product['cost']; ?> 元</td>
                    <td><?php echo $product['stock']; ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <h2>进货</h2>
    <?php foreach ($_SESSION['products'] as $index => $product): ?>
        <form method="post">
            <input type="hidden" name="product_index" value="<?php echo $index; ?>">
            <label for="purchase_quantity_<?php echo $index; ?>">进货 <?php echo $product['name']; ?> 的数量:</label>
            <input type="number" id="purchase_quantity_<?php echo $index; ?>" name="quantity" min="1" required>
            <button type="submit" name="purchase">进货</button>
        </form>
    <?php endforeach; ?>

    <h2>销售</h2>
    <?php foreach ($_SESSION['products'] as $index => $product): ?>
        <form method="post">
            <input type="hidden" name="product_index" value="<?php echo $index; ?>">
            <label for="sell_quantity_<?php echo $index; ?>">销售 <?php echo $product['name']; ?> 的数量:</label>
            <input type="number" id="sell_quantity_<?php echo $index; ?>" name="quantity" min="1" max="<?php echo $product['stock']; ?>" <?php echo $product['stock'] == 0? 'disabled' : ''; ?> required>
            <button type="submit" name="sell">销售</button>
        </form>
    <?php endforeach; ?>
</body>

</html>